<?php

class RatsOverlayTest extends UnitTestCase {
    function TestRatsClientlib() {
        $this->UnitTestCase("Rats Overlay Tests");
    }

    // # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    // # instead we're relying on the fact that methods are executed in declaration order
    // function testSetup() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->createUser(USER, EMAIL, PWD);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    
    function testUploadOverlay() {
      try {
        # upload a overlay
        $rats = new Rats(USER, PWD, BASE);
        $overlay_entry = $rats->createOverlay(OVERLAY_FILENAME, LOCAL_OVERLAY_FILE);
        $this->assertEqual($overlay_entry->content->params->filename->text, OVERLAY_FILENAME);
        # retrieve the upload overlay metadata
        $overlay2_entry = $rats->getOverlayEntry($overlay_entry->id->text);
        $this->assertEqual($overlay_entry->content->params->filename->text, $overlay2_entry->content->params->filename->text);
        # retrieve the upload overlay metadata using the filename as input
        $overlay3_entry = $rats->getOverlayEntryFromId($overlay_entry->content->params->id->text);
        $this->assertEqual($overlay2_entry->content->params->filename->text, $overlay3_entry->content->params->filename->text);
        # delete the uploaded overlay
        $rats->deleteOverlay($overlay_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testUpdateMetadata() {
      try {
        # upload a overlay
        $rats = new Rats(USER, PWD, BASE);
        $overlay_entry = $rats->createOverlay(OVERLAY_FILENAME, LOCAL_OVERLAY_FILE);
        $this->assertEqual($overlay_entry->content->params->filename->text, OVERLAY_FILENAME);
        # retrieve the upload overlay metadata
        $overlay2_entry = $rats->getOverlayEntry($overlay_entry->id->text);
        $this->assertEqual($overlay_entry->content->params->filename->text, $overlay2_entry->content->params->filename->text);

        # change the metadata
        $mode = "1"; 
        $threshold = "FFFFFF";
        $overlay2_entry->content->params->mode->text = $mode;
        $overlay2_entry->content->params->threshold->text = $threshold;
        
        $updated_entry = $overlay2_entry->save();
        $this->assertEqual($updated_entry->content->params->mode->text, $mode);
        $this->assertEqual($updated_entry->content->params->threshold->text, $threshold);
      
        # delete the uploaded overlay
        $rats->deleteOverlay($overlay_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    // function testUploadSameNamedOverlays() {
    //   try {
    //     # upload a overlay
    //     $rats = new Rats(USER, PWD, BASE);
    //     $overlay_entry = $rats->createOverlay(OVERLAY_FILENAME, LOCAL_OVERLAY_FILE);
    //     $this->assertEqual($overlay_entry->content->params->filename->text, OVERLAY_FILENAME);
    //     # upload a second overlay with the same name -> rats should accept and rename
    //     $overlay2_entry = $rats->createOverlay(OVERLAY_FILENAME, LOCAL_OVERLAY_FILE);
    //     $this->assertNotEqual($overlay2_entry->content->params->filename->text, OVERLAY_FILENAME);
    //     # delete the 1st uploaded overlay
    //     $rats->deleteOverlay($overlay_entry->id->text);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 204);
    //     # delete the 2nd uploaded overlay
    //     $rats->deleteOverlay($overlay2_entry->id->text);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 204);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }    

    function testOverlayFeed() {
      try {
        # upload a overlay
        $rats = new Rats(USER, PWD, BASE);
        $overlay_entry = $rats->createOverlay(OVERLAY_FILENAME, LOCAL_OVERLAY_FILE);
        $overlay2_entry = $rats->createOverlay(OVERLAY_FILENAME2, LOCAL_OVERLAY_FILE);
        # get the feed
        $feed = $rats->getOverlayFeed();
        $entry_counter = 0;
        foreach ($feed as $entry) {
          switch($entry->id->text) {
            case $overlay_entry->id->text:
              $entry_counter++;
              break;
            case $overlay2_entry->id->text:
              $entry_counter++;
              break;
            default:
              assert(False);
          }
        }
        $this->assertEqual($entry_counter, 2);
        # delete the 1st uploaded overlay
        $rats->deleteOverlay($overlay_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
        # delete the 2nd uploaded overlay
        $rats->deleteOverlay($overlay2_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    // 
    // function testTeardown() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->deleteUser(USER);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
}
